package blazingbubble.gui.panels;




import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.JPanel;

import spark.network.Sender;
import spark.network.ServerConnection;

import blazingbubble.game.ILobbyGameEventListener;
import blazingbubble.game.command.net.NetGameCommandFactory;
import blazingbubble.gui.INavigationButton;
import blazingbubble.gui.ISwitchable;
import blazingbubble.gui.PanelRegistry;
import blazingbubble.gui.Resources;
import blazingbubble.gui.elements.MenuButton;
import blazingbubble.gui.elements.MenuTextField;


public class JoinIPPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = -8298654971897169906L;

	public static final String PANEL_NAME = "JoinIP";

	private static final int IPFIELD_MAXCHARACTERS = 21;
	private static final float IPFIELD_FONTSIZE = 20;

	private MenuButton backToJoinGameButton = new MenuButton(Resources.BACKBUTTON_IMAGEPATH);
	private MenuButton joinButton = new MenuButton(Resources.JOINBUTTON_IMAGEPATH);

	private MenuTextField ipTextField = new MenuTextField(Resources.TEXTFIELD_BORDERS_IMAGEPATH, IPFIELD_MAXCHARACTERS, Font.BOLD, IPFIELD_FONTSIZE);

	private MenuTextField portTextField = new MenuTextField(Resources.TEXTFIELD_BORDERS_IMAGEPATH, 5, Font.BOLD, 20);

	private ISwitchable parentFrame = null;
	private ILobbyGameEventListener lobbyGameEventListener = null;

	public JoinIPPanel(ISwitchable parentFrame){
		this.parentFrame = parentFrame;
		PanelRegistry.getInstance().registerPanel(PANEL_NAME, this);
		setUp();
	}

	private void setUp(){
		this.setSize(350, 450);
		this.setLayout(null);

		this.add(ipTextField);
		ipTextField.setText(">Enter IP here<");
		ipTextField.setBounds(45, 150, 260, 55);

		this.add(portTextField);
		portTextField.setText(">Enter Port here<");
		portTextField.setBounds(45, 215, 260, 55);

		joinButton.addActionListener(this);
		this.add(joinButton);
		joinButton.setBounds(45, 280, 260, 55);

		backToJoinGameButton.addActionListener(this);
		backToJoinGameButton.setDestinationPanel(PanelRegistry.getInstance().lookUpPanel(JoinGamePanel.PANEL_NAME));
		this.add(backToJoinGameButton);
		backToJoinGameButton.setBounds(45, 345, 260, 55);
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(MainMenuPanel.BACKGROUND_IMAGE, 0, 0, getWidth(), getHeight(), this);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource().equals(backToJoinGameButton)){
			parentFrame.switchPanel(((INavigationButton)event.getSource()).getDestinationPanel());
		}
		else {
			InetSocketAddress serverAddress = null; 
			try {
				serverAddress = new InetSocketAddress(InetAddress.getByName(ipTextField.getText()),
						Integer.parseInt(portTextField.getText()));
				lobbyGameEventListener.joinGameDirectByIP(serverAddress);
			}
			catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
	}

	public void setLobbyGameEventListener(ILobbyGameEventListener lobbyGameEventListener) {
		this.lobbyGameEventListener = lobbyGameEventListener;
	}
}
